package chances.wechat.service.rdb;

import java.rmi.RemoteException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import chances.wechat.entity.StbInfo;
import chances.wechat.entity.WechatStbBind;
import chances.wechat.msggw.context.BindContext;
import chances.wechat.msggw.context.QrBindContext;
import chances.wechat.msggw.context.QrContext;
import chances.wechat.msggw.resp.BaseResponse;
import chances.wechat.msggw.resp.BindResponse;
import chances.wechat.msggw.resp.QrResponse;
import chances.wechat.service.BindService;

@Service("bindService")
public class RdbBindService extends BaseDao implements BindService {

    @Value("${touping.url}")
    private String toupingUrl;

    @SuppressWarnings("unchecked")
    @Override
    public List<WechatStbBind> listBind(String userId) {
        Map<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.put("userId", userId);
        try {
            return (List<WechatStbBind>) this.queryForList("listUserBind",
                    paramsMap);
        } finally {
            paramsMap.clear();
            paramsMap = null;
        }
    }

    @Override
    public WechatStbBind query(String userId, String stbNo) {
        Map<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.put("userId", userId);
        paramsMap.put("stbNo", stbNo);
        try {
            List<WechatStbBind> results = (List<WechatStbBind>) this
                    .queryForList("queryUserBind", paramsMap);
            if (!results.isEmpty()) {
                return results.get(0);
            }
            return null;
        } finally {
            paramsMap.clear();
            paramsMap = null;
        }
    }

    @Override
    public void delBind(Long bindId) {
        Map<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.put("id", bindId);
        try {
            this.delete("deleteBind", paramsMap);
        } finally {
            paramsMap.clear();
            paramsMap = null;
        }
    }

    @Override
    public void unBind(String userId, String stbNo) {
        Map<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.put("userId", userId);
        paramsMap.put("stbNo", stbNo);
        try {
            this.delete("userUnBind", paramsMap);
        } finally {
            paramsMap.clear();
            paramsMap = null;
        }
    }

    @Override
    public void bind(WechatStbBind bind) {
        Map<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.put("obj", bind);
        try {
            this.insert("insertUserBind", paramsMap);
        } finally {
            paramsMap.clear();
            paramsMap = null;
        }
    }

    @Override
    public void updateBindStatus(WechatStbBind bind) {
        Map<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.put("obj", bind);
        try {
            this.insert("updateBindStatus", paramsMap);
        } finally {
            paramsMap.clear();
            paramsMap = null;
        }
    }

    @Override
    public QrResponse getQrCode(QrContext qrContext) throws RemoteException {
        // String url = gwConfig.getBindUrl() + "?" +
        // qrContext.getQuerySring(gwConfig);
        // String signUrl = QrContext.signUrl(gwConfig, url);
        // return super.doPost(qrContext, signUrl, "", QrResponse.class);
        return doNewPost(qrContext, toupingUrl + "/bindmsg/getQrCode",
                QrResponse.class);
    }

    // @Override
    // public BindResponse bindStb(QrContext qrContext) throws RemoteException {
    // String url = gwConfig.getBindUrl() + "?"
    // + qrContext.getQuerySring(gwConfig);
    // String signUrl = QrContext.signUrl(gwConfig, url);
    // return super.doPost(qrContext, signUrl, "", BindResponse.class);
    // }

    @Override
    public BindResponse bind(QrBindContext bindContext) throws RemoteException {
        // String url = bindContext.genUrl(gwConfig);
        // return super.doPost(bindContext, url, "", BindResponse.class);
        return doNewPost(bindContext, toupingUrl + "/bindmsg/bind",
                BindResponse.class);
    }

    @Override
    public BaseResponse unBind(BindContext bindContext) throws RemoteException {
        // String url = gwConfig.getBindUrl() + "?"
        // + bindContext.getUnBindQuerySring(gwConfig);
        // String signUrl = QrContext.signUrl(gwConfig, url);
        // return super.doPost(bindContext, signUrl, "", BaseResponse.class);
        return doNewPost(bindContext, toupingUrl + "/bindmsg/unBind",
                BaseResponse.class);
    }

    @Override
    public StbInfo queryStbInfo(String stbNo) {
        Map<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.put("stbNo", stbNo);
        try {
            return (StbInfo) this.queryForObject("getStbInfoByStbNo",
                    paramsMap);
        } finally {
            paramsMap.clear();
            paramsMap = null;
        }
    }

    @Override
    public void updateBindDesc(String bindId, String desc) {
        Map<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.put("bindId", bindId);
        paramsMap.put("desc", desc);
        try {
            this.update("updateBindDesc", paramsMap);
        } finally {
            paramsMap.clear();
            paramsMap = null;
        }
    }
}
